1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /**
18  * @addtogroup NativeActivity Native Activity
19  * @{
20  */
21 
22 /**
23  * @file native_activity.h
24  */
25 
26 module android.ndk.native_activity;
27 
28 import arsd.jni;
29 import android.ndk;
30 
31 extern (C):
32 nothrow:
33 @nogc:
34 
35 /**
36  * {@link ANativeActivityCallbacks}
37  */
38 
39 /**
40  * This structure defines the native side of an android.app.NativeActivity.
41  * It is created by the framework, and handed to the application's native
42  * code as it is being launched.
43  */
44 struct ANativeActivity
45 {
46     /**
47      * Pointer to the callback function table of the native application.
48      * You can set the functions here to your own callbacks.  The callbacks
49      * pointer itself here should not be changed; it is allocated and managed
50      * for you by the framework.
51      */
52     ANativeActivityCallbacks* callbacks;
53 
54     /**
55      * The global handle on the process's Java VM.
56      */
57     JavaVM* vm;
58 
59     /**
60      * JNI context for the main thread of the app.  Note that this field
61      * can ONLY be used from the main thread of the process; that is, the
62      * thread that calls into the ANativeActivityCallbacks.
63      */
64     JNIEnv* env;
65 
66     /**
67      * The NativeActivity object handle.
68      *
69      * IMPORTANT NOTE: This member is mis-named. It should really be named
70      * 'activity' instead of 'clazz', since it's a reference to the
71      * NativeActivity instance created by the system for you.
72      *
73      * We unfortunately cannot change this without breaking NDK
74      * source-compatibility.
75      */
76     jobject clazz;
77 
78     /**
79      * Path to this application's internal data directory.
80      */
81     const(char)* internalDataPath;
82 
83     /**
84      * Path to this application's external (removable/mountable) data directory.
85      */
86     const(char)* externalDataPath;
87 
88     /**
89      * The platform's SDK version code.
90      */
91     int sdkVersion;
92 
93     /**
94      * This is the native instance of the application.  It is not used by
95      * the framework, but can be set by the application to its own instance
96      * state.
97      */
98     void* instance;
99 
100     /**
101      * Pointer to the Asset Manager instance for the application.  The application
102      * uses this to access binary assets bundled inside its own .apk file.
103      */
104     AAssetManager* assetManager;
105 
106     /**
107      * Available starting with Honeycomb: path to the directory containing
108      * the application's OBB files (if any).  If the app doesn't have any
109      * OBB files, this directory may not exist.
110      */
111     const(char)* obbPath;
112 }
113 
114 /**
115  * These are the callbacks the framework makes into a native application.
116  * All of these callbacks happen on the main thread of the application.
117  * By default, all callbacks are NULL; set to a pointer to your own function
118  * to have it called.
119  */
120 struct ANativeActivityCallbacks
121 {
122     /**
123      * NativeActivity has started.  See Java documentation for Activity.onStart()
124      * for more information.
125      */
126     void function (ANativeActivity* activity) onStart;
127 
128     /**
129      * NativeActivity has resumed.  See Java documentation for Activity.onResume()
130      * for more information.
131      */
132     void function (ANativeActivity* activity) onResume;
133 
134     /**
135      * Framework is asking NativeActivity to save its current instance state.
136      * See Java documentation for Activity.onSaveInstanceState() for more
137      * information.  The returned pointer needs to be created with malloc();
138      * the framework will call free() on it for you.  You also must fill in
139      * outSize with the number of bytes in the allocation.  Note that the
140      * saved state will be persisted, so it can not contain any active
141      * entities (pointers to memory, file descriptors, etc).
142      */
143     void* function (ANativeActivity* activity, size_t* outSize) onSaveInstanceState;
144 
145     /**
146      * NativeActivity has paused.  See Java documentation for Activity.onPause()
147      * for more information.
148      */
149     void function (ANativeActivity* activity) onPause;
150 
151     /**
152      * NativeActivity has stopped.  See Java documentation for Activity.onStop()
153      * for more information.
154      */
155     void function (ANativeActivity* activity) onStop;
156 
157     /**
158      * NativeActivity is being destroyed.  See Java documentation for Activity.onDestroy()
159      * for more information.
160      */
161     void function (ANativeActivity* activity) onDestroy;
162 
163     /**
164      * Focus has changed in this NativeActivity's window.  This is often used,
165      * for example, to pause a game when it loses input focus.
166      */
167     void function (ANativeActivity* activity, int hasFocus) onWindowFocusChanged;
168 
169     /**
170      * The drawing window for this native activity has been created.  You
171      * can use the given native window object to start drawing.
172      */
173     void function (ANativeActivity* activity, ANativeWindow* window) onNativeWindowCreated;
174 
175     /**
176      * The drawing window for this native activity has been resized.  You should
177      * retrieve the new size from the window and ensure that your rendering in
178      * it now matches.
179      */
180     void function (ANativeActivity* activity, ANativeWindow* window) onNativeWindowResized;
181 
182     /**
183      * The drawing window for this native activity needs to be redrawn.  To avoid
184      * transient artifacts during screen changes (such resizing after rotation),
185      * applications should not return from this function until they have finished
186      * drawing their window in its current state.
187      */
188     void function (ANativeActivity* activity, ANativeWindow* window) onNativeWindowRedrawNeeded;
189 
190     /**
191      * The drawing window for this native activity is going to be destroyed.
192      * You MUST ensure that you do not touch the window object after returning
193      * from this function: in the common case of drawing to the window from
194      * another thread, that means the implementation of this callback must
195      * properly synchronize with the other thread to stop its drawing before
196      * returning from here.
197      */
198     void function (ANativeActivity* activity, ANativeWindow* window) onNativeWindowDestroyed;
199 
200     /**
201      * The input queue for this native activity's window has been created.
202      * You can use the given input queue to start retrieving input events.
203      */
204     void function (ANativeActivity* activity, AInputQueue* queue) onInputQueueCreated;
205 
206     /**
207      * The input queue for this native activity's window is being destroyed.
208      * You should no longer try to reference this object upon returning from this
209      * function.
210      */
211     void function (ANativeActivity* activity, AInputQueue* queue) onInputQueueDestroyed;
212 
213     /**
214      * The rectangle in the window in which content should be placed has changed.
215      */
216     void function (ANativeActivity* activity, const(ARect)* rect) onContentRectChanged;
217 
218     /**
219      * The current device AConfiguration has changed.  The new configuration can
220      * be retrieved from assetManager.
221      */
222     void function (ANativeActivity* activity) onConfigurationChanged;
223 
224     /**
225      * The system is running low on memory.  Use this callback to release
226      * resources you do not need, to help the system avoid killing more
227      * important processes.
228      */
229     void function (ANativeActivity* activity) onLowMemory;
230 }
231 
232 /**
233  * This is the function that must be in the native code to instantiate the
234  * application's native activity.  It is called with the activity instance (see
235  * above); if the code is being instantiated from a previously saved instance,
236  * the savedState will be non-NULL and point to the saved data.  You must make
237  * any copy of this data you need -- it will be released after you return from
238  * this function.
239  */
240 alias ANativeActivity_createFunc = void function (
241     ANativeActivity* activity,
242     void* savedState,
243     size_t savedStateSize);
244 
245 /**
246  * The name of the function that NativeInstance looks for when launching its
247  * native code.  This is the default function that is used, you can specify
248  * "android.app.func_name" string meta-data in your manifest to use a different
249  * function.
250  */
251 void ANativeActivity_onCreate ();
252 
253 /**
254  * Finish the given activity.  Its finish() method will be called, causing it
255  * to be stopped and destroyed.  Note that this method can be called from
256  * *any* thread; it will send a message to the main thread of the process
257  * where the Java finish call will take place.
258  */
259 void ANativeActivity_finish (ANativeActivity* activity);
260 
261 /**
262  * Change the window format of the given activity.  Calls getWindow().setFormat()
263  * of the given activity.  Note that this method can be called from
264  * *any* thread; it will send a message to the main thread of the process
265  * where the Java finish call will take place.
266  */
267 void ANativeActivity_setWindowFormat (ANativeActivity* activity, int format);
268 
269 /**
270  * Change the window flags of the given activity.  Calls getWindow().setFlags()
271  * of the given activity.  Note that this method can be called from
272  * *any* thread; it will send a message to the main thread of the process
273  * where the Java finish call will take place.  See window.h for flag constants.
274  */
275 void ANativeActivity_setWindowFlags (
276     ANativeActivity* activity,
277     uint addFlags,
278     uint removeFlags);
279 
280 /**
281  * Flags for ANativeActivity_showSoftInput; see the Java InputMethodManager
282  * API for documentation.
283  */
284 enum
285 {
286     /**
287      * Implicit request to show the input window, not as the result
288      * of a direct request by the user.
289      */
290     ANATIVEACTIVITY_SHOW_SOFT_INPUT_IMPLICIT = 0x0001,
291 
292     /**
293      * The user has forced the input method open (such as by
294      * long-pressing menu) so it should not be closed until they
295      * explicitly do so.
296      */
297     ANATIVEACTIVITY_SHOW_SOFT_INPUT_FORCED = 0x0002
298 }
299 
300 /**
301  * Show the IME while in the given activity.  Calls InputMethodManager.showSoftInput()
302  * for the given activity.  Note that this method can be called from
303  * *any* thread; it will send a message to the main thread of the process
304  * where the Java finish call will take place.
305  */
306 void ANativeActivity_showSoftInput (ANativeActivity* activity, uint flags);
307 
308 /**
309  * Flags for ANativeActivity_hideSoftInput; see the Java InputMethodManager
310  * API for documentation.
311  */
312 enum
313 {
314     /**
315      * The soft input window should only be hidden if it was not
316      * explicitly shown by the user.
317      */
318     ANATIVEACTIVITY_HIDE_SOFT_INPUT_IMPLICIT_ONLY = 0x0001,
319     /**
320      * The soft input window should normally be hidden, unless it was
321      * originally shown with {@link ANATIVEACTIVITY_SHOW_SOFT_INPUT_FORCED}.
322      */
323     ANATIVEACTIVITY_HIDE_SOFT_INPUT_NOT_ALWAYS = 0x0002
324 }
325 
326 /**
327  * Hide the IME while in the given activity.  Calls InputMethodManager.hideSoftInput()
328  * for the given activity.  Note that this method can be called from
329  * *any* thread; it will send a message to the main thread of the process
330  * where the Java finish call will take place.
331  */
332 void ANativeActivity_hideSoftInput (ANativeActivity* activity, uint flags);
333 
334 // ANDROID_NATIVE_ACTIVITY_H
335 
336 /** @} */